package com.cnyz.service.impl;

import com.cnyz.entity.Notice;
import com.cnyz.entity.User;
import com.cnyz.mapper.CustomerMapper;
import com.cnyz.mapper.NoticeMapper;
import com.cnyz.service.NoticeService;
import com.cnyz.vo.NoticeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NoticeServiceImp implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void addNotice(Notice notice) {
        noticeMapper.addNotice(notice);
    }

    @Override
    public void addSomeNotices(List<Notice> notices) {
        noticeMapper.addSomeNotices(notices);
    }

    @Override
    public void updateNotice(Notice notice) {
        noticeMapper.updateNotice(notice);
    }

    @Override
    public void deleteNotice(Long id){
        noticeMapper.deleteNotice(id);
    }

    @Override
    public NoticeVo getNoticeByID(Long id) {
        Notice ret = noticeMapper.getNoticeByID(id);
        NoticeVo vo = new NoticeVo();
        BeanUtils.copyProperties(ret, vo);
        User sender = customerMapper.getById(vo.getSenderID());
        vo.setSenderName(sender.getUsername());
        return vo;
    }

    @Override
    public List<NoticeVo> getNoticesByUserID(Long id){
        List<Notice> ret = noticeMapper.getNoticesByUserID(id);
        List<NoticeVo> vos = new ArrayList<NoticeVo>();
        for(Notice notice : ret){
            NoticeVo vo = new NoticeVo();
            BeanUtils.copyProperties(notice, vo);
            User sender = customerMapper.getById(vo.getSenderID());
            vo.setSenderName(sender.getUsername());
            if(vo.getContent().length()>16){
                vo.setContent(vo.getContent().substring(0,16)+"...");
            }
            vos.add(vo);
        }
        return vos;
    }
}
